package com.taorenqi.business.fragment.mine.insideinfo;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taorenqi.business.R;
import com.taorenqi.business.app.MyApp;
import com.taorenqi.business.fragment.base.BaseBackFragment;
import com.taorenqi.business.widget.statusview.StatusView;
import com.taorenqi.business.widget.subscribers.PageDataSubscriberOnNext;
import com.taorenqi.business.widget.subscribers.ProgressSubscriber;
import com.taorenqi.data.common.BasePageableResponse;
import com.taorenqi.data.entity.mine.MessageDetailEntity;
import com.taorenqi.data.entity.mine.MessageEntity;
import com.taorenqi.data.service.MineDataHttpRequest;

import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 网站公告
 * A simple {@link Fragment} subclass.
 */
public class InfoDetailFragment extends BaseBackFragment {

    public static final String ARG_TYPE = "arg_type";
    public static final String ARG_ID = "arg_id";
    @Bind(R.id.status_view)
    StatusView statusView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.info_detail_title)
    TextView infoDetailTitle;
    @Bind(R.id.info_detail_time)
    TextView infoDetailTime;
    @Bind(R.id.info_detail_content)
    TextView infoDetailContent;

    private int id;
    private int type;
    private BasePageableResponse<MessageDetailEntity> data;
    private PageDataSubscriberOnNext<BasePageableResponse<MessageDetailEntity>> subscriber;

    public static InfoDetailFragment newInstance(int type, int id) {
        // Required empty public constructor
        InfoDetailFragment fragment = new InfoDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_TYPE, type);
        bundle.putInt(ARG_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            id = bundle.getInt(ARG_ID);
            type = bundle.getInt(ARG_TYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_info_detail, container, false);
        ButterKnife.bind(this, view);
        initToolbar();
        initHttpRequestData();
        return attachToSwipeBack(view);
    }

    private void initToolbar() {
        if (type == 1){//网站公告
            toolbar.setTitle("网站公告");
            infoDetailTime.setVisibility(View.VISIBLE);
        }else if (type == 2){//常见问题
            toolbar.setTitle("常见问题");
            infoDetailTime.setVisibility(View.GONE);
        }
        initToolbarNav(toolbar);
    }

    private void initHttpRequestData() {

        subscriber = new PageDataSubscriberOnNext<BasePageableResponse<MessageDetailEntity>>() {
            @Override
            public void onNext(BasePageableResponse<MessageDetailEntity> entity) {
                data = entity;
                infoDetailTitle.setText(data.getDatalist().get(0).getNews_title());
                infoDetailTime.setText(data.getDatalist().get(0).getEditdate());

                CharSequence charSequence= Html.fromHtml(data.getDatalist().get(0).getNews_content());
                infoDetailContent.setText(charSequence);
                //该语句在设置后必加，不然没有任何效果
                infoDetailContent.setMovementMethod(LinkMovementMethod.getInstance());
            }

        };

        MineDataHttpRequest.getInstance(MyApp.getApp()).getMessageDetail(new ProgressSubscriber(subscriber, _mActivity),type,id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
